package com.myschool.server.web.controller;

import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.myschool.server.manager.GradeManager;
import com.myschool.server.manager.StudentsParentsLinkManager;
import com.myschool.server.manager.UserManager;
import com.myschool.server.model.Address;
import com.myschool.server.model.Contacts;
import com.myschool.server.model.Grade;
import com.myschool.server.model.Json;
import com.myschool.server.model.RegistrationStatus;
import com.myschool.server.model.Rows;
import com.myschool.server.model.StudentsParentsLink;
import com.myschool.server.model.User;
import com.myschool.server.model.UserType;

@Controller
public class PrentsController {

	@Autowired
	UserManager userManager;
	
	@Autowired
	StudentsParentsLinkManager studentsParentsLinkManager;
	
	@Autowired
	private GradeManager gradeManager;

	@RequestMapping("getParents.htm")
	public @ResponseBody
	Json createTeacher(HttpServletRequest request, String sidx, String sord,
			int page, int rows, Boolean _search, String searchField,
			String searchOper, String searchString) {

		Json jsonData = new Json();
		try

		{

			String section = request.getParameter("section"); 
			if(section == "")
				return jsonData;
			
			List<StudentsParentsLink> parents = studentsParentsLinkManager.retrieveAllParentsBySection(Long.valueOf(section));
			 
		//	List<User> users =  userManager.retrieveUserByType(UserType.STUDENT);

			int studentCount = parents.size();
 
			jsonData.page = page;
			jsonData.total = studentCount;
			jsonData.record = studentCount;

			jsonData.rows = new Rows[studentCount];
			for (int count = 0; count < studentCount; count++) {
				User student = parents.get(count).getStudent();
				User parent = parents.get(count).getParent();
				
				String parentID = "", parentName = "";
				if(parent != null)
				{
					parentID =   parent.getEntityId().toString();
					parentName = parent.getFirstName();
				}
				
				Rows row = new Rows();
				row.i = count + 1;
				row.cell = new String[] { student.getEntityId().toString(),parentID,
						student.getFirstName() + " " + student.getLastName(),parentName };
				jsonData.rows[count] = row; 
			}

		} catch (Exception ex) {
			System.out.println(ex.getMessage());

		}

		return jsonData;
	}

	@RequestMapping("parentsView.htm")
	public ModelAndView studentView() {
		 
		Set<Grade> gradeList = gradeManager.retrieveAllGrades(1L);
		 ModelAndView view = new ModelAndView("ParentsConfig");
		 view.addObject("gradeList",gradeList);
		 return view;
	}

	@RequestMapping("addEditParents.htm")
	public void addUser(HttpServletRequest request, HttpServletResponse response) {
		try {

			
			//Get parent Record
			User parent = new User();
			
			parent.setFirstName(request.getParameter("ParentFName"));
			parent.setUsername(request.getParameter("ParentFName"));
			parent.setPhoneNumber( request.getParameter("Phone"));
			Address address=new Address();
			Contacts contact=new Contacts();
			contact.setEmail(request.getParameter("EmailAddress"));
			
			//parent.setAddress().setContact().getEmail()( request.getParameter("EmailAddress"));
			 address.setContact(contact);
			 parent.setAddress(address);
			//parent.setId( request.getParameter("parentid"));			
			parent.setRegistrationStatus(RegistrationStatus.PR);
			parent.setType(UserType.FATHER);
			
			//user.setEmailAddress(request.getParameter("EmailAddress")); 
		
			String studentIDStr = request.getParameter("studentID");
			String parentIdStr = request.getParameter("parentsID");
			Long studentID = Long.parseLong(studentIDStr);  
			//Insert parent
			if(parentIdStr.isEmpty())
			{
				System.out.println("Creating Parent");
				parent  =  this.userManager.create(parent); 
				
				User student =   new User();
				student.setEntityId(studentID);
				student  =  this.userManager.retrieve(student); 
				
				
				//Link the student and parent 
				StudentsParentsLink link = new  StudentsParentsLink();
				link.setParent(parent);
				
				link.setStudent(student);
				studentsParentsLinkManager.create(link);
				
			}
			else
			{
				 this.userManager.update(parent); 
			}
			 
		
			// System.out.println(user.getFirstName());
		} catch (Exception ex) {
			System.out.println(ex.getMessage());

		}

	}

	@RequestMapping("deleteParent.htm")
	public void deleteUser(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			String userIDParameter = request.getParameter("userID"); 
			String[] userIDArray = userIDParameter.split(";");
			User user;
			for (String userID : userIDArray) {
				if (userID == "")
					continue;
				
				user = new User();
				Long id = Long.parseLong(userID.trim());
				user.setEntityId(id);
				this.userManager.delete(user);
			}

		} catch (Exception ex) {
			System.out.println(ex.getMessage()); 
		} 
	} 


}
